<?php
/**
 * Created by PhpStorm.
 * User: lzy 林子彦
 * Date: 2018/6/1
 * Time: 上午1:09
 */
namespace App\Admin\Controllers;
use App\Admin\Contracts\MemberLevelInterface;
use App\Base\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


/**
 * 用户层级
 * Class MenuController
 * @package App\Admin\Controllers
 */
class MemberLevelController extends Controller
{
    /**
     * 广告接口
     * @return MemberLevelInterface
     */
    public function getMemberLevelInterface()
    {
        return app()->make(MemberLevelInterface::class);
    }

    /**
     * 广告首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        if($request->getMethod()=='POST'){
            $where=function($query) use ($request){
                $title=$request->input("title");
                if(!empty($title)){
                    $query->where("title","like",'%'.$title.'%');
                }
                $type=$request->input("type");
                if(!empty($type)){
                    $query->where("type",$type);
                }
            };
            return  $this->getMemberLevelInterface()->getList($where);
        }
        return view("admin.memberlevel.index");
    }

    /**
     * 广告编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */

    public function edit(Request $request)
    {
        $lid=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'level_name' => 'required',
                'id' => 'required',
            ],["level_name.required"=>'层级名称是必须',"id.required"=>'ID必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getMemberLevelInterface()->saveData($request->all());
        }
        $data=$this->getMemberLevelInterface()->getInfo(["id"=>$lid]);
        return view("admin.memberlevel.edit")->with("data",$data);
    }

    /**
     * 广告添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {

        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'level_name' => 'required',

            ],["level_name.required"=>'层级名称是必须']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getMemberLevelInterface()->saveData($request->all());
        }
        return view("admin.memberlevel.add");
    }

    /**
     * 广告删除
     * @param Request $request
     */
    public function del(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where=function($query) use ($request){
                $id=$request->input("id");
                $query->where("id",$id);
            };
            return  $this->getMemberLevelInterface()->deleteData($where);
        }
    }


}